/*
 * << Haru Free PDF Library >> -- hpdf_ext_text.c
 *
 * URL: http://libharu.org
 *
 * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
 * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.
 * It is provided "as is" without express or implied warranty.
 *
 */

#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_pages.h"
#include "hpdf.h"
#include "dataproc.h"
#include "../ext/hpdf_ext.h"

/*****************************************************************************
 * @brief   自定义输出
 * @return  执行状态
 *****************************************************************************/
void hpdf_custom(HPDF_Page page, const char *cmd)
{
	HPDF_STATUS ret;
	HPDF_PageAttr attr;

	attr = (HPDF_PageAttr)page->attr;

	if ((ret = HPDF_Stream_WriteStr(attr->stream, cmd)) != HPDF_OK)
		HPDF_CheckError(page->error);
}

/*****************************************************************************
 * @brief   自定义输出
 * @return  执行状态
 *****************************************************************************/
void hpdf_custom_with_par(
	HPDF_Page page,
	const char *cmd,
	ii32 par,
	uu8 ip_n)
{
	HPDF_STATUS ret;
	HPDF_PageAttr attr;
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;

	attr = (HPDF_PageAttr)page->attr;

	// 数字转换
	length += i32_to_string(&pdf_buf[0], par, ip_n, 0x03);
	length += copy_str(cmd, &pdf_buf[length], HPDF_TMP_BUF_SIZ - 12);

	// 发送
	if ((ret = HPDF_Stream_WriteStr(attr->stream, pdf_buf)) != HPDF_OK)
		HPDF_CheckError(page->error);
}

void hpdf_custom_cfg(
	hpdf_custom_par_type *par,
	const char *text,
	ii16 val,
	uu8 ip_n,
	uu8 attr)
{
	par->text = text;
	par->val = val;
	par->attr = attr;
	par->ip_n = ip_n;
}

void hpdf_custom_exe(HPDF_Page page, hpdf_custom_par_type *par, ii8 max)
{
	HPDF_STATUS ret;
	HPDF_PageAttr attr;
	char pdf_buf[HPDF_TMP_BUF_SIZ];
	char length = 0;

	attr = (HPDF_PageAttr)page->attr;

	ii8 cnt;

	for (cnt = 0; cnt < max; cnt++)
	{
		// 执行
		if ((par[cnt].attr & HPDF_CMD_NoExe) == 0)
		{
			// 添加头
			if (par[cnt].text && ((par[cnt].attr & HPDF_CMD_TFore) != 0))
			{
				length += copy_str(par[cnt].text, &pdf_buf[length], 511 - length);
			}

			if ((par[cnt].attr & HPDF_CMD_NoNum) == 0)
			{
				length += i32_to_string(&pdf_buf[length], par[cnt].val, par[cnt].ip_n, 0x03);
			}

			// 添加尾
			if (par[cnt].text && ((par[cnt].attr & HPDF_CMD_TFore) == 0))
			{
				length += copy_str(par[cnt].text, &pdf_buf[length], 511 - length);
			}
		}

		// 结束
		if (par[cnt].attr & HPDF_CMD_End)
		{
			break;
		}
	}

	pdf_buf[length] = 0;

	// 发送
	if ((ret = HPDF_Stream_WriteStr(attr->stream, pdf_buf)) != HPDF_OK)
		HPDF_CheckError(page->error);
}

/*****************************************************************************
 * @brief   栈管理
 * @param   page       ：句柄
 * @return  执行状态
 *****************************************************************************/
uu32 hpdf_page_push(HPDF_Page page)
{
	HPDF_STATUS ret = HPDF_Page_CheckState(page, HPDF_GMODE_PAGE_DESCRIPTION |
													 HPDF_GMODE_TEXT_OBJECT);
	HPDF_PageAttr attr;

	HPDF_PTRACE((" HPDF_Page_SetDash\n"));

	if (ret != HPDF_OK)
		return ret;

	attr = (HPDF_PageAttr)page->attr;

	if ((ret = HPDF_Stream_WriteStr(attr->stream, "q\n")) != HPDF_OK)
		return HPDF_CheckError(page->error);

	return ret;
}

uu32 hpdf_page_pop(HPDF_Page page)
{
	HPDF_STATUS ret = HPDF_Page_CheckState(page, HPDF_GMODE_PAGE_DESCRIPTION |
													 HPDF_GMODE_TEXT_OBJECT);
	HPDF_PageAttr attr;

	HPDF_PTRACE((" HPDF_Page_SetDash\n"));

	if (ret != HPDF_OK)
		return ret;

	attr = (HPDF_PageAttr)page->attr;

	if ((ret = HPDF_Stream_WriteStr(attr->stream, "Q\n")) != HPDF_OK)
		return HPDF_CheckError(page->error);

	return ret;
}
